package com.example.ui.register;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.NetRequest;
import com.example.ui.learn.ListeningActivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Register2Fm extends Fragment {
	View v;
	private EditText ra2_tellphone;
	public  String tellphone;
	private Button ra2_return, ra2_next;
	private TextView ra2_title;
	private Boolean isChecked=false;
	private Boolean isValid=false;
	String data;
	String res;

	private Builder bulider;
	private AlertDialog infoad;
	private Button ra2_check;
	final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
			99999999, 999999999, Integer.MAX_VALUE };
	public static boolean canNext = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.a_register2, null);
		init(v);
		return v;
	}
	 private void next(){
				Register3Fm register3=new Register3Fm(tellphone);
				FragmentManager fm = getFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				ft.replace(R.id.l,register3);
				ft.addToBackStack(null);
				ft.commit();			
	 }

	private void init(View v) {
		ra2_tellphone = (EditText) v.findViewById(R.id.ar2_tellphone);
		ra2_return = (Button) v.findViewById(R.id.register_return);
		ra2_next = (Button) v.findViewById(R.id.register_next);
		ra2_title = (TextView) v.findViewById(R.id.register_title);
		ra2_title.setText("验证手机(2/5)");
		ra2_check=(Button)v.findViewById(R.id.check);
		ra2_return.setOnClickListener(ra2_ocl);
		ra2_next.setOnClickListener(ra2_ocl);
		ra2_check.setOnClickListener(ra2_ocl);
	}

	Runnable r2_run = new Runnable() {
		@Override
		public void run() {
			res = NetRequest.checkMobile(tellphone);
			JSONObject jsonObject;
			isChecked=true;
			try {
				jsonObject = new JSONObject(res);
				String response = jsonObject.getString("response");
				if (response.equals("false")) { // 资讯未改变
					isValid=true;
					new Thread(r1_run).start();
					h.sendEmptyMessage(1);
				} else if (response.equals("true")) {
					isValid=false;
					h.sendEmptyMessage(2);
				}
			} catch (JSONException e) {

				e.printStackTrace();
			}

		}
	};
	private Runnable r1_run = new Runnable() {
		@Override
		public void run() {
			res = NetRequest.postRequest2(tellphone);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(res);
				String response = jsonObject.getString("response");
				if (response.equals("success")) { // 资讯未改变
					
					h.sendEmptyMessage(3);
				} else if (response.equals("user_error")) {
					isValid=false;
					h.sendEmptyMessage(4);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};
	private Handler h = new Handler() {
		

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				infoad.dismiss();
				Toast.makeText(getActivity(), "验证成功，正在发送短信！", Toast.LENGTH_LONG).show();
				break;
			case 2:
				infoad.dismiss();
				new AlertDialog.Builder(getActivity())
				.setTitle("此号码已注册,请重新输入")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).show();
				break;
			case 3:
				infoad.dismiss();
				Toast.makeText(getActivity(), "发送短信成功，请注意查收！", Toast.LENGTH_LONG).show();
				break;
			case 4:
				infoad.dismiss();
				Toast.makeText(getActivity(),"短信发送失败，请验证号码！", Toast.LENGTH_SHORT);
				break;
			default:
				break;
			}
		};
	};

	OnClickListener ra2_ocl = new OnClickListener() {


		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.register_next:
				if(!isChecked){
					new AlertDialog.Builder(getActivity())
					.setTitle("请先验证手机号码")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
								}
							}).show();
					return;
				}
				if(!isValid){
					new AlertDialog.Builder(getActivity())
					.setTitle("此号码已注册请更换号码")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									isChecked=false;
								}
							}).show();
					return;
				}
				next(); 
				isChecked=false;
				isValid=false;
				break;
			case R.id.register_return:

				getActivity().onBackPressed();
				break;
			case R.id.check:
				bulider = new AlertDialog.Builder(getActivity());
				bulider.setView(
						View.inflate(getActivity(), R.layout.onloading, null))
						.create();
				infoad = bulider.show();
				tellphone=ra2_tellphone.getText().toString();
				int result = tellphone.length();
				if("".equals(tellphone)||result!=11){
					infoad.dismiss();
					Toast.makeText(getActivity(), "请输入正确手机号码，我们将会向您发送一条短信",
							Toast.LENGTH_LONG).show();
					ra2_tellphone.setText("");
					return;
				}
				new Thread(r2_run).start();
				break;
			default:
				break;
			}
		}
	};

}
